﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RM2012Server.Model;
using System.Data;
using RM2012Server.Business;
using DevComponents.DotNetBar.Controls;
using System.Windows.Forms;

namespace RM2012Server.Controller
{
    public class UnitCtrl
    {
        UnitDAO m_UnitDAO = new UnitDAO();

        public void LoadToComboBox(ComboBoxEx cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "UnitName";
            cmb.ValueMember = "UnitId";
        }

        public void LoadToDataGridViewComboBoxColumn(DataGridViewComboBoxColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "UnitName";
            cmb.ValueMember = "UnitId";
            cmb.DataPropertyName = "UnitId";
            cmb.HeaderText = "Đơn vị";
        }

        public void LoadToDataGridViewComboBoxExColumn(DataGridViewComboBoxExColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "UnitName";
            cmb.ValueMember = "UnitId";
            cmb.DataPropertyName = "UnitId";
            cmb.HeaderText = "Đơn vị";
        }

        public void Show(DataGridViewX dgv, BindingNavigator bn)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();
            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public void Show(DataGridViewX dgv,
                         BindingNavigator bn,
                         TextBoxX txtUnitId,
                         TextBoxX txtUnitName,
                         TextBoxX txtRemark)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();

            txtUnitId.DataBindings.Clear();
            txtUnitId.DataBindings.Add("Text", bs, "UnitId");

            txtUnitName.DataBindings.Clear();
            txtUnitName.DataBindings.Add("Text", bs, "UnitName");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public void ShowWithSources(DataGridViewX dgv,
                         BindingNavigator bn,
                         TextBoxX txtUnitId,
                         TextBoxX txtUnitName,
                         TextBoxX txtRemark,
                         DataTable dtSource)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = dtSource;

            txtUnitId.DataBindings.Clear();
            txtUnitId.DataBindings.Add("Text", bs, "UnitId");

            txtUnitName.DataBindings.Clear();
            txtUnitName.DataBindings.Add("Text", bs, "UnitName");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public DataTable GetAll()
        {
            return m_UnitDAO.GetAll();
        }

        public DataTable GetById(String unitId)
        {
            return m_UnitDAO.GetById(unitId);
        }

        public DataTable GetByLikeId(String unitId)
        {
            return m_UnitDAO.GetByLikeId(unitId);
        }

        public DataTable GetByName(String stateName)
        {
            return m_UnitDAO.GetByName(stateName);
        }

        public int Add(UnitInfo info)
        {
            return m_UnitDAO.Add(info);
        }

        public int Update(UnitInfo info)
        {
            return m_UnitDAO.Update(info);
        }

        public int Delete(String unitId)
        {
            return m_UnitDAO.Delete(unitId);
        }

        public DataRow AddNewRow()
        {
            return m_UnitDAO.AddNewRow();
        }

        public void AddNewUnit(DataRow m_Row)
        {
            m_UnitDAO.AddNewUnit(m_Row);
        }

        public bool SaveData()
        {
            return m_UnitDAO.SaveData();
        }
    }
}
